<?php

class AdminModel extends AuthModel
{
	private $oPlayerList;
	
	public function __construct()
	{
		parent::__construct();
		
		// actions
		if (Input::hasRequest("action"))
		{
			$id = Input::request("id");
			$player = PlayerRegistry::i()->byId($id);
			if (Input::hasRequest("id") && (is_null($player) || $this->player() === $player))
			{
				$this->addError("player_id", "PLAYER_NOT_FOUND_BY_ID");
			}
			else
			{
				if (Input::request("action") == "ch_ac_level")
				{
					$acLevel = Input::request("ac_level");
					
					$player->setAcLevel($acLevel);
				}
				else if (Input::request("action") == "delete")
				{
					PlayerRegistry::i()->removeById($id);
					$this->setMsg(array("type" => "for_player", "text" => "PLAYER_REMOVED", "player" => $player));
				}
				else if (Input::request("action") == "ch_pass")
				{
					$password = Input::request("new_pass");
					$repeat = Input::request("new_pass_repeat");
					
					$passLen = strlen($password);
					if ($passLen < 3 || $passLen > 16)
						$this->addError("password", "PASSWORD_ERROR");
					else if ($password != $repeat)
						$this->addError("repeat_password", "REPEAT_PASSWORD_ERROR");
					
					if (!$this->hasError())
					{
						$player->setPassword($password);
						$this->setMsg(array("type" => "for_player", "text" => "PASS_OFF_CHANGED", "player" => $player));
					}
				}
			}
		}
		$this->oPlayerList = PlayerRegistry::i()->playerList();
		asort($this->oPlayerList);
	}
	
	public function playerList() { return $this->oPlayerList; }
	
}

?>